package com.yuexun.dhdoctor.widget.model;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.bean.model.GridSele;
import com.yuexun.dhdoctor.base.bean.model.HelpFileData;
import com.yuexun.dhdoctor.baselistener.StringListener;
import com.yuexun.dhdoctor.widget.FlowRadioGroup;
import com.yuexun.dhdoctor.widget.pickerUtil.ModelDatePicker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by chenss on 2017/7/31 0031.
 */

public class CreateRadioView {

    Context context;
    ArrayList<HelpFileData> dtlList;//配置详情列表
    ModelDatePicker customDatePicker1;

    SimpleDateFormat sdf = null;
    String isDate;
    List<HelpFileData> alreadyDtlList;
    boolean isBackfill;

    public CreateRadioView(Context context, ArrayList<HelpFileData> dtlList, List<HelpFileData> alreadyDtlList, boolean isBackfill) {
        this.context = context;
        this.dtlList = dtlList;
        this.alreadyDtlList = alreadyDtlList;
        this.isBackfill = isBackfill;
    }

    /**
     * 方法描述：从布局中添加单选布局
     *
     * @param fileData
     * @return
     */
    public View createRadioView(final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_01, null);//也可以从XML中加载布局

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);

        txt_title.setText(fileData.getDtlName());

        HelpFileData cont = new HelpFileData();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList().get(0);
                }
            }
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        fileD.setShowType("2");
        dtlList.add(fileD);

        final List<GridSele> list = new ArrayList<GridSele>();
        for (int i = 0; i < fileData.getList().size(); i++) {
            GridSele gridSele = new GridSele();
            gridSele.setDtlName(fileData.getList().get(i).getDtlName());
            if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                gridSele.setSelect(true);

                helpFileDatas.clear();
                HelpFileData f = new HelpFileData();
                f.setDtlName(cont.getDtlName());
                f.setDtlValue(cont.getDtlValue());
                f.setShowType(fileData.getShowType());
                helpFileDatas.add(f);
                fileD.setList(helpFileDatas);
            } else {
                gridSele.setSelect(false);
            }
            list.add(gridSele);
        }

        for (final GridSele sele : list) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setText(sele.getDtlName());
            radioButton.setChecked(sele.isSelect());
            Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(null, null, drawable, null);
            radioButton.setButtonDrawable(null);
            radioButton.setCompoundDrawablePadding(10);
            radioButton.setPadding(0, 15, 20, 0);
            viewGroup.addView(radioButton);

            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    sele.setSelect(isChecked);

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(sele.getDtlName());
                    f.setDtlValue(sele.getDtlName());
                    f.setShowType(fileData.getShowType());
                    helpFileDatas.add(f);
                    fileD.setList(helpFileDatas);
                }
            });

        }

        return view;
    }

    /**
     * 方法描述：从布局中添加单选+文本布局
     *
     * @param fileData
     * @return
     */
    public View createRadio2View(String num, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = null;
        if (num.equals("2")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_02, null);//也可以从XML中加载布局
        } else if (num.equals("7")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_07, null);//也可以从XML中加载布局
        }

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView defined_title = (TextView) view.findViewById(R.id.defined_title);
        final FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);
        final EditText defined_txt = (EditText) view.findViewById(R.id.defined_txt);

        txt_title.setText(fileData.getDtlName());

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        fileD.setShowType("2");
        dtlList.add(fileD);

        HelpFileData cont = new HelpFileData();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList().get(0);
                }
            }
        }

        final List<GridSele> list = new ArrayList<GridSele>();
        if (fileData.getList().size() == 1) {
            if (!fileData.getList().get(0).getDtlName().isEmpty()) {
                GridSele gridSele = new GridSele();
                gridSele.setDtlName(fileData.getList().get(0).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    gridSele.setSelect(true);

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(cont.getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                } else {
                    gridSele.setSelect(false);
                }
                list.add(gridSele);
            }
        } else {
            if (num.equals("2")) {
                for (int i = 0; i < fileData.getList().size() - 1; i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                        gridSele.setSelect(true);

                        helpFileDatas.clear();
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.getDtlName());
                        f.setDtlValue(cont.getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(fileData.getList().size() - 1).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(fileData.getList().size() - 1).getDtlName())){
                    defined_txt.setText(cont.getDtlValue());

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(fileData.getList().get(fileData.getList().size() - 1).getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            } else if (num.equals("7")) {
                for (int i = 1; i < fileData.getList().size(); i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                        gridSele.setSelect(true);

                        helpFileDatas.clear();
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.getDtlName());
                        f.setDtlValue(cont.getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(0).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    defined_txt.setText(cont.getDtlValue());

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(fileData.getList().get(0).getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            }
        }

        for (final GridSele sele : list) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setText(sele.getDtlName());
            radioButton.setChecked(sele.isSelect());
            Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(null, null, drawable, null);
            radioButton.setButtonDrawable(null);
            radioButton.setCompoundDrawablePadding(10);
            radioButton.setPadding(0, 15, 20, 0);
            viewGroup.addView(radioButton);

            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    defined_txt.setText("");
                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(sele.getDtlName());
                    f.setDtlValue(sele.getDtlName());
                    f.setShowType(fileData.getShowType());
                    helpFileDatas.add(f);
                    fileD.setList(helpFileDatas);
                }
            });
        }


        defined_txt.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                /**加这个判断，防止该事件被执行两次*/
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setSelect(false);
                        RadioButton button = (RadioButton) viewGroup.getChildAt(i);
                        button.setChecked(false);
                    }

                    if (fileD.getList().size() > 0) {
                        fileD.getList().get(0).setDtlValue(defined_txt.getText().toString());
                        fileD.getList().get(0).setDtlName(defined_title.getText().toString());
                    } else {
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(defined_title.getText().toString());
                        f.setDtlValue(defined_txt.getText().toString());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    }

                }
                return false;
            }
        });
        defined_txt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!defined_txt.getText().toString().isEmpty()) {
                    if (fileD.getList().size() > 0) {
                        fileD.getList().get(0).setDtlValue(defined_txt.getText().toString());
                        fileD.getList().get(0).setDtlName(defined_title.getText().toString());
                    } else {
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(defined_title.getText().toString());
                        f.setDtlValue(defined_txt.getText().toString());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    }
                }
            }
        });

        return view;
    }

    /**
     * 方法描述：从布局中添加单选 + 时间
     *
     * @param fileData
     * @return
     */
    public View createRadio3View(String num, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        View view = null;
        if (num.equals("3") || num.equals("4")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_03, null);//也可以从XML中加载布局
        } else if (num.equals("8") || num.equals("9")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_08, null);//也可以从XML中加载布局
        }

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView defined_title = (TextView) view.findViewById(R.id.defined_title);
        final FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);
        final TextView date_view = (TextView) view.findViewById(R.id.date_view);

        txt_title.setText(fileData.getDtlName());

        if (num.equals("3") || num.equals("8")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            isDate = "3";
        } else if (num.equals("4") || num.equals("9")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            isDate = "4";
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        fileD.setShowType("2");
        dtlList.add(fileD);

        HelpFileData cont = new HelpFileData();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList().get(0);
                }
            }
        }

        final List<GridSele> list = new ArrayList<GridSele>();
        if (fileData.getList().size() == 1) {
            if (!fileData.getList().get(0).getDtlName().isEmpty()) {
                GridSele gridSele = new GridSele();
                gridSele.setDtlName(fileData.getList().get(0).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    gridSele.setSelect(true);

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(cont.getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                } else {
                    gridSele.setSelect(false);
                }
                list.add(gridSele);
            }
        } else {
            if (num.equals("3") || num.equals("4")) {
                for (int i = 0; i < fileData.getList().size() - 1; i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                        gridSele.setSelect(true);

                        helpFileDatas.clear();
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.getDtlName());
                        f.setDtlValue(cont.getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(fileData.getList().size() - 1).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(fileData.getList().size() - 1).getDtlName())){
                    date_view.setText(cont.getDtlValue());

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(fileData.getList().get(fileData.getList().size() - 1).getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            } else if (num.equals("8") || num.equals("9")) {
                for (int i = 1; i < fileData.getList().size(); i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                        gridSele.setSelect(true);

                        helpFileDatas.clear();
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.getDtlName());
                        f.setDtlValue(cont.getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(0).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    date_view.setText(cont.getDtlValue());

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(fileData.getList().get(0).getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            }
        }

        for (final GridSele sele : list) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setText(sele.getDtlName());
            radioButton.setChecked(sele.isSelect());
            Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(null, null, drawable, null);
            radioButton.setButtonDrawable(null);
            radioButton.setCompoundDrawablePadding(10);
            radioButton.setPadding(0, 15, 20, 0);
            viewGroup.addView(radioButton);

            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    date_view.setText("");
                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(sele.getDtlName());
                    f.setDtlValue(sele.getDtlName());
                    f.setShowType(fileData.getShowType());
                    helpFileDatas.add(f);
                    fileD.setList(helpFileDatas);
                }
            });
        }


        date_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(date_view, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelect(false);
                            RadioButton button = (RadioButton) viewGroup.getChildAt(i);
                            button.setChecked(false);
                        }

                        if (fileD.getList().size() > 0) {
                            fileD.getList().get(0).setDtlValue(position);
                            fileD.getList().get(0).setDtlName(defined_title.getText().toString());
                        } else {
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(defined_title.getText().toString());
                            f.setDtlValue(position);
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                        }
                    }
                });
            }
        });
        return view;
    }

    /**
     * 方法描述：从布局中添加单选 + 时间范围
     *
     * @param fileData
     * @return
     */
    public View createRadio4View(String num, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        View view = null;
        if (num.equals("5") || num.equals("6")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_05, null);//也可以从XML中加载布局
        } else if (num.equals("10") || num.equals("11")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_layout_10, null);//也可以从XML中加载布局
        }

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView defined_title = (TextView) view.findViewById(R.id.defined_title);
        final FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);
        final TextView startDate = (TextView) view.findViewById(R.id.start_date_view);
        final TextView endDate = (TextView) view.findViewById(R.id.end_date_view);

        txt_title.setText(fileData.getDtlName());

        if (num.equals("5") || num.equals("10")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            isDate = "3";
        } else if (num.equals("6") || num.equals("11")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            isDate = "4";
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        fileD.setShowType("2");
        dtlList.add(fileD);

        HelpFileData cont = new HelpFileData();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList().get(0);
                }
            }
        }

        final List<GridSele> list = new ArrayList<GridSele>();
        if (fileData.getList().size() == 1) {
            if (!fileData.getList().get(0).getDtlName().isEmpty()) {
                GridSele gridSele = new GridSele();
                gridSele.setDtlName(fileData.getList().get(0).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    gridSele.setSelect(true);

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(cont.getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                } else {
                    gridSele.setSelect(false);
                }
                list.add(gridSele);
            }
        } else {
            if (num.equals("5") || num.equals("6")) {
                for (int i = 0; i < fileData.getList().size() - 1; i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                        gridSele.setSelect(true);

                        helpFileDatas.clear();
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.getDtlName());
                        f.setDtlValue(cont.getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(fileData.getList().size() - 1).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(fileData.getList().size() - 1).getDtlName())){
                    String[] time = cont.getDtlValue().replace("&nbsp;", " ").split(" -- ");
//                    String[] time = fileData.getList().get(fileData.getList().size() - 1).getDtlValue().split("\\-\\- ");
                    startDate.setText(time[0]);
                    endDate.setText(time[1]);

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(fileData.getList().get(fileData.getList().size() - 1).getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            } else if (num.equals("10") || num.equals("11")) {
                for (int i = 1; i < fileData.getList().size(); i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill && cont.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                        gridSele.setSelect(true);

                        helpFileDatas.clear();
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.getDtlName());
                        f.setDtlValue(cont.getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(0).getDtlName());
                if(isBackfill && cont.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    String[] time = cont.getDtlValue().replace("&nbsp;", " ").split(" -- ");
                    startDate.setText(time[0]);
                    endDate.setText(time[1]);

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(cont.getDtlName());
                    f.setDtlValue(cont.getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            }
        }

        for (final GridSele sele : list) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setText(sele.getDtlName());
            radioButton.setChecked(sele.isSelect());
            Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(null, null, drawable, null);
            radioButton.setButtonDrawable(null);
            radioButton.setCompoundDrawablePadding(10);
            radioButton.setPadding(0, 15, 20, 0);
            viewGroup.addView(radioButton);

            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    startDate.setText("");
                    endDate.setText("");

                    helpFileDatas.clear();
                    HelpFileData f = new HelpFileData();
                    f.setDtlName(sele.getDtlName());
                    f.setDtlValue(sele.getDtlName());
                    f.setShowType(fileData.getShowType());
                    helpFileDatas.add(f);
                    fileD.setList(helpFileDatas);
                }
            });
        }

        startDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(startDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelect(false);
                            RadioButton button = (RadioButton) viewGroup.getChildAt(i);
                            button.setChecked(false);
                        }

                        if (fileD.getList().size() > 0) {
                            fileD.getList().get(0).setDtlValue(position + " -- " + endDate.getText().toString());
                            fileD.getList().get(0).setDtlName(defined_title.getText().toString());
                        } else {
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(defined_title.getText().toString());
                            f.setDtlValue(position + " -- " + endDate.getText().toString());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                        }
                    }
                });
            }
        });
        endDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(endDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelect(false);
                            RadioButton button = (RadioButton) viewGroup.getChildAt(i);
                            button.setChecked(false);
                        }

                        if (fileD.getList().size() > 0) {
                            fileD.getList().get(0).setDtlValue(startDate.getText().toString() + " -- " + position);
                            fileD.getList().get(0).setDtlName(defined_title.getText().toString());
                        } else {
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(defined_title.getText().toString());
                            f.setDtlValue(startDate.getText().toString() + " -- " + position);
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                        }
                    }
                });
            }
        });
        return view;
    }

    private void initDatePicker(final TextView tv, final String num, final StringListener listener) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());

        customDatePicker1 = new ModelDatePicker(context, new ModelDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                tv.setText(time);
                listener.getBack(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecific(num);
        customDatePicker1.setIsLoop(false); // 不允许循环滚动
        customDatePicker1.show(now);
    }
}
